package cn.tj212.xiaoyin.common.storage;


import cn.tj212.xiaoyin.common.util.PropertiesUtil;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.util.*;

public class QiniuWrapper {

    private static final Logger logger=LoggerFactory.getLogger(QiniuWrapper.class);

    private static final String CONFIG_BUCKET="";
    private static final String CONFIG_AK="";
    private static final String CONFIG_SK="";
    private static final String CONFIG_CDN="";

    private static final Auth auth;
    private static final UploadManager uploadManager;

    private static final String bucketName;
    private static final List<String> cdns;

    /**
     * 从外部文件中获取七牛云存储的相关配置
     */
    static {
        Properties properties=PropertiesUtil.getDefaultProperties();
        auth=Auth.create(properties.getProperty(CONFIG_AK),properties.getProperty(CONFIG_SK));
        Configuration cfg=new Configuration(Zone.zone2());//设置空间上传域名
        uploadManager=new UploadManager(cfg);
        bucketName=properties.getProperty(CONFIG_BUCKET);
        String cdn=properties.getProperty(CONFIG_CDN);
        cdns=Arrays.asList(cdn.split(","));
    }

    /**
     * 获取上传资源的token
     * @return
     */
    public static String getUploadToken(){
        return auth.uploadToken(bucketName);
    }

    /**
     * 获取更新资源的token，只能用于更新参数key所代表的资源
     * @param key 存储空间中已经存在的资源key
     * @return
     */
    public static String getUploadToken(String key){
        return auth.uploadToken(bucketName,key);
    }

    /**
     * 上传图片
     * @param data 二进制格式的文件内容
     * @param key 文件在七牛中的key
     * @param update 是否是更新操作
     * @return
     */
    public static String upload(byte[] data,String key,boolean update){
        try {
            String token=update?auth.uploadToken(bucketName,key):auth.uploadToken(bucketName);
            Response response=uploadManager.put(data,getFullKey(data,key),token);
            DefaultPutRet ret=response.jsonToObject(DefaultPutRet.class);
            return ret.key;
        }catch (QiniuException e){
            logger.error("upload file to qiniu cloud storage failed",e);
        }
        return null;
    }

    private static String getFullKey(byte[] data, String key) {
        FileType type=FileTypeHelper.getType(data);
        if (type!=null){
            return key+"."+type.name().toLowerCase();
        }else {
            return key;
        }
    }


    public static String getUrl(String key,String model){
        return getUrl(key, model,3600);
    }

    public static String getUrl(String key,long expires){
        if (!StringUtils.isEmpty(key)){
            long time=System.currentTimeMillis()/1000+expires;
            return auth.privateDownloadUrl("http://"+getCDN()+"/@"+key,time);
        }
        return null;
    }

    public static String getUrl(String key, String model, long expires) {
        if (StringUtils.hasText(model)){
            return auth.privateDownloadUrl("http://"+getCDN()+"/@"+key+"?"+model,expires);
        }else {
            return auth.privateDownloadUrl("http://"+getCDN()+"/@"+key,expires);
        }
    }

    public static String getUrl(String key) {
        if (StringUtils.isEmpty(key)){
            return auth.privateDownloadUrl("http://"+getCDN()+"/@"+key);
        }
        return null;
    }

    /**
     * 获取多个key图片
     * @param keys
     * @param model
     * @return
     */
    public static List<String> getUrls(String keys,String model){
        List<String> list=null;
        if (org.apache.commons.lang.StringUtils.isNotBlank(keys)){
            list=new ArrayList<>();
            for (String key:keys.split(",")
                 ) {
                list.add(getUrl(key));
            }
        }
        return list;
    }

    /**
     * 从多条CDN中随机选择一条
     * @return
     */
    public static String getCDN(){
        Random random=new Random();
        int num=random.nextInt();
        return cdns.get(num);
    }
}
